Image Processing

The SDK provides abundant image processing APIs for displaying image, saving image, converting pixel format, reconstructing image, recording, etc. After images are acquired, they can be passed into these APIs for image processing.

This chapter includes:

Image Rendering

After acquiring the images, you can refer to the following sample codes and call MV_CC_DisplayOneFrameEx2() to display the image on the specified window.
displayimage = MV_CC_IMAGE()
displayimage.nWidth = stOutFrame.stFrameInfo.nWidth
displayimage.nHeight = stOutFrame.stFrameInfo.nHeight
displayimage.enPixelType = stOutFrame.stFrameInfo.enPixelType
displayimage.pImageBuf = stOutFrame.pBufAddr
displayimage.nImageBuffSize = stOutFrame.stFrameInfo.nFrameLen
displayimage.nImageLen = stOutFrame.stFrameInfo.nFrameLen
ret = cam.MV_CC_DisplayOneFrameEx2(int(hwnd),displayimage, 0)
if ret != 0:
print ("MV_CC_DisplayOneFrameEx2 fail! ret[0x%x]" % ret)
  1. Get width, height, pixel format, buffer and length of the image.
  2. Configure image rendering mode. Set the mode to 0 (default mode), indicating GDI mode.
  3. Render acquired image through specified mode by calling MV_CC_DisplayOneFrameEx2().


Image Saving

You can save the image to the local or the memory.
The following sample code shows how to saves images as local BMP files.
# Save images in BMP format
c_file_path = file_path.encode('ascii')
stSaveParam = MV_SAVE_IMAGE_TO_FILE_PARAM_EX()
stSaveParam.enPixelType = frame_info.stFrameInfo.enPixelType # Image pixel format
stSaveParam.nWidth = frame_info.stFrameInfo.nWidth # Image width
stSaveParam.nHeight = frame_info.stFrameInfo.nHeight # Image height
stSaveParam.nDataLen = frame_info.stFrameInfo.nFrameLen
stSaveParam.pData = frame_info.pBufAddr
stSaveParam.enImageType = MV_Image_Bmp # Image format for saving
stSaveParam.pcImagePath = create_string_buffer(c_file_path)
stSaveParam.iMethodValue = 1
stSaveParam.nQuality = 80 # JPG: (50,99], invalid in other format
ret = cam.MV_CC_SaveImageToFileEx(stSaveParam)


Pixel Format Conversion

Call MV_CC_ConvertPixelTypeEx() to convert image format into another format.
For instance, you can transform Bayer to RGB/BGR. The interpolation to images in Bayer format supports several algorithms such as smoothing filter, Gamma correction, and CCM correction. Refer to details in Image Processing.
The following sample code shows how to convert images to RGB Format.
nRGBSize = stOutFrame.stFrameInfo.nWidth * stOutFrame.stFrameInfo.nHeight * 3
stConvertParam = MV_CC_PIXEL_CONVERT_PARAM_EX()
memset(byref(stConvertParam), 0, sizeof(stConvertParam))
stConvertParam.nWidth = stOutFrame.stFrameInfo.nWidth
stConvertParam.nHeight = stOutFrame.stFrameInfo.nHeight
stConvertParam.pSrcData = stOutFrame.pBufAddr
stConvertParam.nSrcDataLen = stOutFrame.stFrameInfo.nFrameLen
stConvertParam.enSrcPixelType = stOutFrame.stFrameInfo.enPixelType
stConvertParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed
stConvertParam.pDstBuffer = (c_ubyte * nRGBSize)()
stConvertParam.nDstBufferSize = nRGBSize
ret = cam.MV_CC_ConvertPixelTypeEx(stConvertParam)
if ret != 0:
print ("convert pixel fail! ret[0x%x]" % ret)
sys.exit()


Image Decoding

Some cameras support encoding and compressing images. Users can decode image by calling MV_CC_HBDecode().
Attention
Check if the input image is complete before decoding. If some data packet of image is lost, the API will return the failure message. Check with nLostPacket (the number of packets lost in the current frame) in MV_FRAME_OUT_INFO_EX . If the value of nLostPacket is greater than 0, there is packet loss.
The following sample codes show how to decode compressed image.
# Decode parameters
stDecodeParam = MV_CC_HB_DECODE_PARAM()
# Get the size of the data packet
stParam = MVCC_INTVALUE()
memset(byref(stParam), 0, sizeof(stParam))
ret = cam.MV_CC_GetIntValue("PayloadSize", stParam)
if 0 != ret:
print("Get PayloadSize fail! ret[0x%x]" % ret)
return ret
nPayloadSize = stParam.nCurValue
stDecodeParam.pSrcBuf = frame_info.pBufAddr
stDecodeParam.nSrcLen = frame_info.stFrameInfo.nFrameLen
stDecodeParam.pDstBuf = (c_ubyte * nPayloadSize)()
stDecodeParam.nDstBufSize = nPayloadSize
ret = cam.MV_CC_HBDecode(stDecodeParam)
if ret != 0:
print("HB Decode fail! ret[0x%x]" % ret)
return ret


Record

You can save the image received by the camera as video file through SDK.
  1. Call MV_CC_StartRecord() to start recording. You need to configure parameters required for recording.
  2. Call MV_CC_InputOneFrame() to add image data to video files repeatedly.
  3. Call MV_CC_StopRecord() to stop recording when you finish.
The following sample codes show how to save image as video file.
  1. Start recording: Call MV_CC_StartRecord() to configure parameters that can be acquired via camera or image. The following sample codes show how to get parameters including width, height, and pixel format from camera.

    stParam = MVCC_INTVALUE()
    memset(byref(stParam), 0, sizeof(MVCC_INTVALUE))
    stRecordPar = MV_CC_RECORD_PARAM()
    memset(byref(stRecordPar), 0, sizeof(MV_CC_RECORD_PARAM))
    # Get the width of the image
    ret = cam.MV_CC_GetIntValue("Width", stParam)
    if ret != 0:
    print ("get width fail! nRet [0x%x]" % ret)
    sys.exit()
    stRecordPar.nWidth = stParam.nCurValue
    # Get the height of the image
    ret = cam.MV_CC_GetIntValue("Height", stParam)
    if ret != 0:
    print ("get height fail! nRet [0x%x]"% ret)
    sys.exit()
    stRecordPar.nHeight = stParam.nCurValue
    # Get the pixel format of the image
    stEnumValue = MVCC_ENUMVALUE()
    memset(byref(stEnumValue), 0 ,sizeof(MVCC_ENUMVALUE))
    ret = cam.MV_CC_GetEnumValue("PixelFormat", stEnumValue)
    if ret != 0:
    print ("get PixelFormat fail! nRet [0x%x]" % ret)
    sys.exit()
    stRecordPar.enPixelType = MvGvspPixelType(stEnumValue.nCurValue)
    # Get the frame rate of the image
    stFloatValue = MVCC_FLOATVALUE()
    memset(byref(stFloatValue), 0 ,sizeof(MVCC_FLOATVALUE))
    ret = cam.MV_CC_GetFloatValue("ResultingFrameRate", stFloatValue)
    if ret != 0:
    print ("get ResultingFrameRate value fail! nRet [0x%x]" % ret)
    sys.exit()
    stRecordPar.fFrameRate = stFloatValue.fCurValue
    # Video structure assignment
    stRecordPar.nBitRate = 1000
    stRecordPar.enRecordFmtType = MV_FormatType_AVI
    stRecordPar.strFilePath= 'Recording.avi'.encode('ascii')
    # Start recording
    nRet = cam.MV_CC_StartRecord(stRecordPar)
    if ret != 0:
    print ("Start Record fail! nRet [0x%x]\n", nRet)
    sys.exit()
  2. Add image data to video files: Call MV_CC_InputOneFrame() repeatedly to add image data from the camera to video files.

    # Customize the thread function
    def work_thread(cam=0, pData=0, nDataSize=0):
    stOutFrame = MV_FRAME_OUT()
    memset(byref(stOutFrame), 0, sizeof(stOutFrame))
    stInputFrameInfo = MV_CC_INPUT_FRAME_INFO()
    memset(byref(stInputFrameInfo), 0 ,sizeof(MV_CC_INPUT_FRAME_INFO))
    while True:
    ret = cam.MV_CC_GetImageBuffer(stOutFrame, 1000)
    if None != stOutFrame.pBufAddr and 0 == ret:
    print ("get one frame: Width[%d], Height[%d], nFrameNum[%d]" % (stOutFrame.stFrameInfo.nWidth, stOutFrame.stFrameInfo.nHeight, stOutFrame.stFrameInfo.nFrameNum))
    stInputFrameInfo.pData = cast(stOutFrame.pBufAddr, POINTER(c_ubyte))
    stInputFrameInfo.nDataLen = stOutFrame.stFrameInfo.nFrameLen
    # Input a frame of data to the video interface
    ret = cam.MV_CC_InputOneFrame(stInputFrameInfo)
    if ret != 0:
    print ("input one frame fail! nRet [0x%x]" % ret)
    nRet = cam.MV_CC_FreeImageBuffer(stOutFrame)
    else:
    print ("no data[0x%x]" % ret)
    if g_bExit == True:
    break
  3. Stop recording: Call MV_CC_StopRecord() to stop recording.

    # Stop recording
    ret = cam.MV_CC_StopRecord()
    if ret != 0:
    print ("stop Record fail! ret[0x%x]" % ret)
    sys.exit()


Image Splitting via Multi-Light Control

Split Image via SDK
Some cameras support multi-light control, and can send the entire multi-light control image to SDK. By calling MV_CC_ReconstructImage(), the entire image can be split and each image under exposure can be outputted.
The basic steps of image division via SDK and image reconstruction are as follows:
  1. Call MV_CC_GetEnumValue() to get the value of node "MultiLightControl", namely the number of exposures of the current camera.
    Attention
    • Some cameras may lack the node "MultiLightControl", so you need to configure the number of exposures.
    • In the HB (high bandwidth) mode, the value of node "MultiLightControl" needs to be converted for effective number of exposures.
  2. If the image acquired is HB image, call MV_CC_HBDecode() for HB decoding.
  3. Call MV_CC_ReconstructImage() to reconstruct image.
The sample code is as follows:
# Reconstruct image parameters
stReconstructParam = MV_RECONSTRUCT_IMAGE_PARAM()
# If the image is in HB format, it should be decoded first
if stOutFrame.stFrameInfo.enPixelType in HB_format_list:
# Gets the size of the data packet.
stParam = MVCC_INTVALUE()
memset(byref(stParam), 0, sizeof(stParam))
ret = cam.MV_CC_GetIntValue("PayloadSize", stParam)
if 0 != ret:
raise Exception("Get PayloadSize fail! ret[0x%x]" % ret)
nPayloadSize = stParam.nCurValue
stDecodeParam.pSrcBuf = stOutFrame.pBufAddr
stDecodeParam.nSrcLen = stOutFrame.stFrameInfo.nFrameLen
stDecodeParam.pDstBuf = (c_ubyte * nPayloadSize)()
stDecodeParam.nDstBufSize = nPayloadSize
ret = cam.MV_CC_HBDecode(stDecodeParam)
if ret != 0:
raise Exception("HB Decode fail! ret[0x%x]" % ret)
else:
stReconstructParam.nWidth = stDecodeParam.nWidth
stReconstructParam.nHeight = stDecodeParam.nHeight
stReconstructParam.enPixelType = stDecodeParam.enDstPixelType
stReconstructParam.pSrcData = stDecodeParam.pDstBuf
stReconstructParam.nSrcDataLen = stDecodeParam.nDstBufLen
else:
stReconstructParam.nWidth = stOutFrame.stFrameInfo.nWidth
stReconstructParam.nHeight = stOutFrame.stFrameInfo.nHeight
stReconstructParam.enPixelType = stOutFrame.stFrameInfo.enPixelType
stReconstructParam.pSrcData = stOutFrame.pBufAddr
stReconstructParam.nSrcDataLen = stOutFrame.stFrameInfo.nFrameLen
stReconstructParam.nExposureNum = exposure_num
stReconstructParam.enReconstructMethod = MV_SPLIT_BY_LINE
dst_buffer_len = int(stReconstructParam.nSrcDataLen/exposure_num)
for i in range(exposure_num):
dst_buffer = (c_ubyte * dst_buffer_len)()
dst_buffer_list.append(dst_buffer)
stReconstructParam.stDstBufList[i].pBuf = dst_buffer_list[i]
stReconstructParam.stDstBufList[i].nBufSize = dst_buffer_len
# Image reconstruction
ret = cam.MV_CC_ReconstructImage(stReconstructParam)
if ret != 0:
raise Exception("MV_CC_ReconstructImage fail! ret[0x%x]" % ret)
else:
print("Reconstruct image success")



Previous: Image Acquisition Next: None